﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Dashboard
{
    /// <summary>
    /// Interaction logic for Log.xaml
    /// </summary>
    /// 
    public delegate void TableUpdate();
    public delegate object GetInstance();

    public class NotificationItem
    {
        public NotificationItem(string time, string taskname)
        {
            Time = time;
            TaskName = taskname;
        }
        public NotificationItem()
        {
            ;
        }
        public string Time { set; get; }
        public string TaskName { set; get; }
    }

    public class OperationItem
    {
        //
        public OperationItem(string time,string taskname, string done,string delayto,string cancel)
        {
            Time = time;
            TaskName = taskname;
            Done = done;
            DelayTo = delayto;
            Cancel = cancel;
        }

        public string Time { set; get; }
        public string TaskName { set; get; }
        public string Done { set; get; }
        public string DelayTo { set; get; }
        public string Cancel { set; get; }

    }

    public partial class LogView : UserControl
    {
        
        int NotificationCount;
        int OperationCount;
        Delegate Notifupdate;
        Delegate Operaupdate;
        Dashboard.logdataDataSetTableAdapters.NotificationTableAdapter newNotificationAdaptor;
        Dashboard.logdataDataSetTableAdapters.OperationTableAdapter newOperationAdaptor;
               
        static LogView instance;
        public static LogView GetLogView()
        {
            if(instance == null)
            {
                instance = new LogView();
            }
            return instance;
        }

        private LogView()
        {     
            newNotificationAdaptor = new Dashboard.logdataDataSetTableAdapters.NotificationTableAdapter();
            newOperationAdaptor = new Dashboard.logdataDataSetTableAdapters.OperationTableAdapter();
            Notifupdate = new TableUpdate(notificationgrid_update);
            Operaupdate = new TableUpdate(operationgrid_update);
            InitializeComponent();
        }


        public void notificationgrid_update()
        {
            //

            logdataDataSet.NotificationRow work_row;

            int work_currentcount = newNotificationAdaptor.GetData().Rows.Count;

            if (NotificationCount < work_currentcount)
            {
                work_row = (logdataDataSet.NotificationRow)newNotificationAdaptor.GetData().Rows[NotificationCount++];
                notificationgrid.Items.Insert(0, new NotificationItem(work_row.Time, work_row.TaskName));
            }

            this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, Notifupdate);
        }

        private void notificationgrid_Initialized(object sender, EventArgs e)
        {
            // Binding data by name
            foreach (DataGridTextColumn col in notificationgrid.Columns)
            {
                col.Binding = new Binding((string)col.Header);
            }

            //
            NotificationCount = 0;
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, Notifupdate);

        }

        private void operationgrid_Initialized(object sender, EventArgs e)
        {
            // Binding data by name
            foreach (DataGridTextColumn col in operationgrid.Columns)
            {
                col.Binding = new Binding((string)col.Header);
            }
            OperationCount = 0;
            operationgrid.Dispatcher.BeginInvoke(DispatcherPriority.Normal, Operaupdate);


        }



        public void operationgrid_update()
        {
            logdataDataSet.OperationRow work_row;
            int work_currentcount = newOperationAdaptor.GetData().Rows.Count;
            if(OperationCount < work_currentcount)
            {
                work_row = (logdataDataSet.OperationRow)newOperationAdaptor.GetData().Rows[OperationCount++];
                operationgrid.Items.Insert(0, new OperationItem(work_row.Time, work_row.TaskName, work_row.Done, work_row.DelayTo, work_row.Cancel));
            }
            operationgrid.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, Operaupdate);
        }



    }

}
